var $table = $('#table');
var URI_LIST = "/sec/page_ele/list";
var URI_CREATE = "/sec/page_ele/create";
var URI_UPDATE = "/sec/page_ele/update/";
var URI_DEL = "/sec/page_ele/delete/";
var URI_DETAIL="/sec/page_ele/detail/";
var createFrom = "#SecPageElecreateForm";
var updateForm = "#SecPageEleupdateForm";
var createValidation =null;
var updateValidation =null;
var fieldCloumns = [
    {field: 'ck', checkbox: true},
                    {field: 'pageId', title:'主键'},
                            {field: 'eleId', title:'页面元素'},
                            {field: 'elDesc', title:'元素说明'},
                            {field: 'pid', title:'对应权限ID'},
                {field: 'action', title:'操作', align:'center', formatter:'actionFormatter', events:'actionEvents', clickToSelect:false}
]

                function initValidate(formId) {
    var validator = $(formId).validate({
        rules: {
                                                                                                    eleId: {required: true, maxlength:100},                                                                                                            elDesc: {required: true, maxlength:100},                                                                                                            pid: {required: true, digits:turue}                                                        }
    });
    return validator;
}
var idField = "pageId";
$(function () {
    $table.bootstrapTable({
        url: URI_LIST,
        detailFormatter: 'detailFormatter',
        idField:idField,
        toolbar: '#toolbar',
        columns: fieldCloumns
    });
});
function detailFormatter(index, row) {
    $.get(URI_DETAIL+row.pageId+".html",function(resp){
        $("tr[data-index="+index+"]").next(".detail-view").children("td").html(resp);
    });
}
// 格式化操作按钮
function actionFormatter(value, row, index) {
    return [
        '<a class="update" href="javascript:;" onclick="updateAction()" data-toggle="tooltip" title="Edit"><i class="glyphicon glyphicon-edit"></i></a>　',
        '<a class="delete" href="javascript:;" onclick="deleteAction()" data-toggle="tooltip" title="Remove"><i class="glyphicon glyphicon-remove"></i></a>'
    ].join('');
}
// 新增
var createDialog;
function createAction() {

    createDialog = $.dialog({
        animationSpeed: 300,
        title: '新增',
        columnClass: 'col-md-10',
        content: 'url:' + URI_CREATE+".html",
        onContentReady: function () {
            initMaterialInput();
            createValidation = initValidate(createFrom)
            $(createFrom).ajaxForm({
                target: '#btn-SecPageEle-submit',   // target element(s) to be updated with server response
                dataType: 'json',
                url: URI_CREATE,
                beforeSubmit: function (formData, jqForm, options) {
                    return createValidation.valid();
                },
                success: function (result, statusText, xhr, $form) {
                    if (result.code != "0000") {
                        $.confirm({
                            theme: 'dark',
                            animation: 'rotateX',
                            closeAnimation: 'rotateX',
                            title: false,
                            content: result.data.errorMsg || result.desc,
                            buttons: {
                                confirm: {
                                    text: '确认',
                                    btnClass: 'waves-effect waves-button waves-light'
                                }
                            }
                        });
                    } else {
                        createDialog.close();
                        $table.bootstrapTable('refresh');
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.confirm({
                        theme: 'dark',
                        animation: 'rotateX',
                        closeAnimation: 'rotateX',
                        title: false,
                        content: textStatus,
                        buttons: {
                            confirm: {
                                text: '确认',
                                btnClass: 'waves-effect waves-button waves-light'
                            }
                        }
                    });
                }
            });
        }
    });
}
// 编辑
var updateDialog;
function updateAction() {
    var rows = $table.bootstrapTable('getSelections');
    if (rows.length != 1) {
        $.confirm({
            title: false,
            content: '请选择一条记录！',
            autoClose: 'cancel|3000',
            backgroundDismiss: true,
            buttons: {
                cancel: {
                    text: '取消',
                    btnClass: 'waves-effect waves-button'
                }
            }
        });
    } else {
        updateDialog = $.dialog({
            animationSpeed: 300,
            title: '编辑',
            columnClass: 'col-md-10',
            content: 'url:' + URI_UPDATE + rows[0].pageId + ".html",
            onContentReady: function () {
                initMaterialInput();
            updateValidation = initValidate(updateForm)
            $(updateForm).ajaxForm({
                target: '#btn-SecPageEle-update',   // target element(s) to be updated with server response
                dataType: 'json',
                url: URI_UPDATE+rows[0].pageId,
                beforeSubmit: function (formData, jqForm, options) {
                    return updateValidation.valid();
                },
                success: function (result, statusText, xhr, $form) {
                    if (result.code != "0000") {
                        $.confirm({
                            theme: 'dark',
                            animation: 'rotateX',
                            closeAnimation: 'rotateX',
                            title: false,
                            content: result.data.errorMsg || result.desc,
                            buttons: {
                                confirm: {
                                    text: '确认',
                                    btnClass: 'waves-effect waves-button waves-light'
                                }
                            }
                        });
                    } else {
                        updateDialog.close();
                        $table.bootstrapTable('refresh');
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.confirm({
                        theme: 'dark',
                        animation: 'rotateX',
                        closeAnimation: 'rotateX',
                        title: false,
                        content: textStatus,
                        buttons: {
                            confirm: {
                                text: '确认',
                                btnClass: 'waves-effect waves-button waves-light'
                            }
                        }
                    });
                }
            });
            }
        });
    }
}
// 删除
var deleteDialog;
function deleteAction() {
    var rows = $table.bootstrapTable('getSelections');
    if (rows.length == 0) {
        $.confirm({
            title: false,
            content: '请至少选择一条记录！',
            autoClose: 'cancel|3000',
            backgroundDismiss: true,
            buttons: {
                cancel: {
                    text: '取消',
                    btnClass: 'waves-effect waves-button'
                }
            }
        });
    } else {
        deleteDialog = $.confirm({
            type: 'red',
            animationSpeed: 300,
            title: false,
            content: '确认删除该记录吗？',
            buttons: {
                confirm: {
                    text: '确认',
                    btnClass: 'waves-effect waves-button',
                    action: function () {
                        var ids = new Array();
                        for (var i in rows) {
                            ids.push(rows[i].pageId);
                        }
                        $.ajax({
                            type: 'get',
                            url: URI_DEL + ids.join("-"),
                            success: function (result) {
                                if (result.code != "0000") {
                                    $.confirm({
                                        theme: 'dark',
                                        animation: 'rotateX',
                                        closeAnimation: 'rotateX',
                                        title: false,
                                        content: result.desc,
                                        buttons: {
                                            confirm: {
                                                text: '确认',
                                                btnClass: 'waves-effect waves-button waves-light'
                                            }
                                        }
                                    });
                                } else {
                                    deleteDialog.close();
                                    $table.bootstrapTable('refresh');
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                $.confirm({
                                    theme: 'dark',
                                    animation: 'rotateX',
                                    closeAnimation: 'rotateX',
                                    title: false,
                                    content: textStatus,
                                    buttons: {
                                        confirm: {
                                            text: '确认',
                                            btnClass: 'waves-effect waves-button waves-light'
                                        }
                                    }
                                });
                            }
                        });
                    }
                },
                cancel: {
                    text: '取消',
                    btnClass: 'waves-effect waves-button'
                }
            }
        });
    }
}